package cn.cs.yangtai.entity.vo;

import com.github.pagehelper.Page;
import lombok.Data;
import org.springframework.lang.Nullable;

import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@Data
public class PageResult <T>{
    private final Integer pageNum;
    private final Integer pageSize;
    private final Long total;
    private final List<T> list;
    /**
     * 包装page对象
     */
    public PageResult(@NotNull Integer pageNum, @NotNull Integer pageSize,
                      @NotNull Long total,@Nullable List<T> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.list = list;
    }
    /**
     * 包装page对象
     */
    public static <T> PageResult<T> restPage(@NotNull Integer pageNum, @NotNull Integer pageSize,
                                             @NotNull Long total,
                                             @Nullable List<T> list){
        return new PageResult<>(pageNum,pageSize,total,
                Optional.ofNullable(list).orElse(Collections.emptyList()));
    }
    /**
     * 包装page对象
     */
    public static <T> PageResult<T> restPage(@NotNull Page<T> page ){
        return new PageResult<>(page.getPageNum(),page.getPageSize(),
                page.getTotal(),page.getResult());
    }


}